package CCRMClient.GUI;

import javax.swing.JPanel;

import java.awt.Color;

import javax.swing.border.LineBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.SwingConstants;
import javax.swing.JTextField;

import java.awt.SystemColor;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import CCRMClient.ClientStart;
import CCRMClient.Controllers.TemplateManager;
import Common.SERVER_COMMANDS;
import Common.Entities.MarketingTemplate;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import net.proteanit.sql.DbUtils;

import org.eclipse.wb.swing.FocusTraversalOnArray;

import java.awt.Component;
import java.io.IOException;
import java.sql.ResultSet;

import javax.swing.ListSelectionModel;
/**
 * This class is a screen of changing a status of marketing template
 * @author AL
 *
 */
public class TemplateChangeStatusScreen extends JPanel {
	private JLabel lblChangeTempStatus;
	private JLabel lblTempStatus;
	private JTextField textSearchField;
	private JButton btnSearch;
	private JButton btnBack;
	private JButton btnViewTemp;
	private JTable tblTemplates;
	private JScrollPane scpTemplates;
	private JLabel lblSearchResults;

	/**
	 * Create the panel.
	 */
	public TemplateChangeStatusScreen () {
		initialize();
	}
	
	public TemplateChangeStatusScreen (ResultSet searchResults, String search) {
		this();
		textSearchField.setText(search);
		loadRStoTable(searchResults);
		tblTemplates.setEnabled(true);
		tblTemplates.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		btnViewTemp.setEnabled(true);
	}
	
	private void loadRStoTable(ResultSet searchResults) {
		tblTemplates.setModel(DbUtils.resultSetToTableModel(searchResults));
		JTableHeader th = tblTemplates.getTableHeader();
		TableColumnModel tcm = th.getColumnModel();
		TableColumn tc = tcm.getColumn(0);
		tc.setHeaderValue("Template ID");
		
		th = tblTemplates.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(1);
		tc.setHeaderValue("Name");
		
		th = tblTemplates.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(2);
		tc.setHeaderValue("Description");
		
		th = tblTemplates.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(3);
		tc.setHeaderValue("Message");
		
		th = tblTemplates.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(4);
		tc.setHeaderValue("Product ID");
		
		th = tblTemplates.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(5);
		tc.setHeaderValue("Status");
		
		tblTemplates.getColumnModel().getColumn(0).setResizable(false);
		tblTemplates.getColumnModel().getColumn(0).setPreferredWidth(50);
		tblTemplates.getColumnModel().getColumn(0).setMinWidth(7);
		tblTemplates.getColumnModel().getColumn(2).setResizable(false);
		tblTemplates.getColumnModel().getColumn(2).setPreferredWidth(80);
		tblTemplates.getColumnModel().getColumn(2).setMinWidth(10);
		tblTemplates.getColumnModel().getColumn(4).setPreferredWidth(40);
		
		th.repaint();
	}
	private void initialize() {
		setSize(637, 577);
		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		setBackground(Color.WHITE);
		setLayout(null);
		add(getLblChangeTempStatus());
		add(getLblTempStatus());
		add(getTextSearchField());
		add(getBtnSearch());
		add(getLblSearchResults());
		add(getScpTemplates());
		add(getBtnBack());
		add(getBtnViewTemp());
	}
	JLabel getLblChangeTempStatus() {
		if (lblChangeTempStatus == null) {
			lblChangeTempStatus = new JLabel("Update template Status");
			lblChangeTempStatus.setHorizontalAlignment(SwingConstants.CENTER);
			lblChangeTempStatus.setForeground(Color.RED);
			lblChangeTempStatus.setFont(new Font("David", Font.BOLD, 26));
			lblChangeTempStatus.setBounds(0, 11, 637, 27);
		}
		return lblChangeTempStatus;
	}
	JLabel getLblTempStatus() {
		if (lblTempStatus == null) {
			lblTempStatus = new JLabel("Template Status:");
			lblTempStatus.setFont(new Font("David", Font.BOLD, 20));
			lblTempStatus.setBounds(10, 45, 199, 29);
		}
		return lblTempStatus;
	}
	JTextField getTextSearchField() {
		if (textSearchField == null) {
			textSearchField = new JTextField();
			textSearchField.setColumns(10);
			textSearchField.setBackground(SystemColor.menu);
			textSearchField.setBounds(214, 49, 251, 20);
		}
		return textSearchField;
	}
	JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("Search");
			btnSearch.addActionListener(new BtnSearchActionListener());
			btnSearch.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnSearch.setBounds(475, 48, 124, 23);
		}
		return btnSearch;
	}
	JButton getBtnBack() {
		if (btnBack == null) {
			btnBack = new JButton("Back");
			btnBack.addActionListener(new BtnBackActionListener());
			btnBack.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnBack.setBounds(10, 494, 124, 23);
		}
		return btnBack;
	}
	JButton getBtnViewTemp() {
		if (btnViewTemp == null) {
			btnViewTemp = new JButton("View Template");
			btnViewTemp.addActionListener(new BtnViewTemplateActionListener());
			btnViewTemp.setEnabled(false);
			btnViewTemp.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnViewTemp.setBounds(475, 495, 124, 23);
		}
		return btnViewTemp;
	}
	
	JTable getTblTemplates() {
		if (tblTemplates == null) {
			tblTemplates = new JTable();
			tblTemplates.setBackground(SystemColor.menu);
			tblTemplates.setFont(new Font("David", Font.BOLD, 11));
			tblTemplates.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			tblTemplates.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
			tblTemplates.setModel(new DefaultTableModel(
				new Object[][] {
					{null, null, null, null, null, null},
				},
				new String[] {
					"TemplateID", "Name", "Description", "Message", "Product ID", "Status"
				}
			) {
				Class[] columnTypes = new Class[] {
					String.class, String.class, String.class, String.class, String.class,  String.class
				};
				public Class getColumnClass(int columnIndex) {
					return columnTypes[columnIndex];
				}
				boolean[] columnEditables = new boolean[] {
						false, false, false, false, false
					};
					@Override
					public boolean isCellEditable(int row, int column) {
						return false;
					}
			});
			tblTemplates.getColumnModel().getColumn(0).setResizable(false);
			tblTemplates.getColumnModel().getColumn(0).setPreferredWidth(50);
			tblTemplates.getColumnModel().getColumn(0).setMinWidth(7);
			tblTemplates.getColumnModel().getColumn(2).setResizable(false);
			tblTemplates.getColumnModel().getColumn(2).setPreferredWidth(80);
			tblTemplates.getColumnModel().getColumn(2).setMinWidth(10);
			tblTemplates.getColumnModel().getColumn(4).setPreferredWidth(40);
		}
		return tblTemplates;
	}
	
	JScrollPane getScpTemplates() {
		if (scpTemplates == null) {
			scpTemplates = new JScrollPane();
			scpTemplates.setViewportBorder(null);
			scpTemplates.setBounds(10, 131, 589, 327);
			scpTemplates.setViewportView(getTblTemplates());
		}
		return scpTemplates;
	}
	
	JLabel getLblSearchResults() {
		if (lblSearchResults == null) {
			lblSearchResults = new JLabel("Search Results:");
			lblSearchResults.setHorizontalAlignment(SwingConstants.LEFT);
			lblSearchResults.setForeground(Color.BLACK);
			lblSearchResults.setFont(new Font("David", Font.BOLD, 22));
			lblSearchResults.setBounds(10, 85, 199, 27);
		}
		return lblSearchResults;
	}
	/**
	 * This inner class implements event listener for pressing Back button
	 * @author AL
	 *
	 */
	private class BtnBackActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			ClientStart.panelSwicher(new TemplateMarketingManagerScreen());
		}
	}
	/**
	 * This inner class implements event listener for pressing Search button
	 * @author AL
	 *
	 */
	private class BtnSearchActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			try {
				String search = textSearchField.getText();
				if (search.isEmpty()) throw new NullPointerException();
				try {
					ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.SEARCH_TEMPLATE_BY_STATUS, TemplateManager.SearchTemplateByStatus(SERVER_COMMANDS.SEARCH_TEMPLATE_BY_STATUS, search));
				} catch (IOException exp) {
					JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
					ClientStart.quitApp();
				}
			} catch (NullPointerException exp) {
				JOptionPane.showMessageDialog(null, "Fill in template status", "Search Templates", JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}
	
	/**
	 * This inner class implements event listener for pressing view Marketing Template
	 * @author AL
	 *
	 */
	private class BtnViewTemplateActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			int rowIdx = tblTemplates.getSelectedRow();
			if (rowIdx == -1)
				JOptionPane.showMessageDialog(null, "Please select template", "View template", JOptionPane.INFORMATION_MESSAGE);
			else {
				String tempName;
				long templateID;
				String tempID;
				String tempDesc;
				String ProdID;
				String tempMsg;
				String tempStatus;
				
				DefaultTableModel dtm = (DefaultTableModel) tblTemplates.getModel();
				Object tmpID = dtm.getValueAt(rowIdx, 0);
				templateID = (Long)tmpID;
				tempID = Long.toString(templateID);
				tempName = null;
				tempDesc = null;
				ProdID = "0";
				tempMsg = null;
				tempStatus = null;
				try {
					ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.VIEW_TEMPLATE, TemplateManager.ViewTemplate(tempID,tempName,tempDesc, tempMsg, tempStatus, ProdID));
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
					ClientStart.quitApp();
				}
			} 
		} 
	}
}
